#ifndef __XML_PARSER_SCAN_H__
#define __XML_PARSER_SCAN_H__

#include "typedefs.h"
#include "errordef.h"
#include <stdio.h>

#define LF_CHAR                     '\n'

#define STATE_START                 0
#define STATE_NAME                  1

#define STATE_LEFT_BRACKET          2
#define STATE_BACKSLASH             3
#define STATE_EXCLAIM               5
#define STATE_1ST_MINUS             6
#define STATE_2ND_MINUS             7
#define STATE_1ST_LEFT_SQUARE       8
#define STATE_C                     9
#define STATE_D                     10
#define STATE_1ST_A                 11
#define STATE_T                     12
#define STATE_2ND_A                 13
#define STATE_2ND_LEFT_SQUARE       14

#define STATE_SINGLE_QUOTE          15
#define STATE_DOUBLE_QUOTE          16

#define STATE_QUESTION_MARK         17
#define STATE_1ST_RIGHT_SQUARE      18
#define STATE_2ND_RIGHT_SQUARE      19
#define STATE_ACCEPT                20
#define STATE_3RD_MINUS             21
#define STATE_4TH_MINUS             22
#define STATE_DOCTYPE_D             23
#define STATE_DOCTYPE_O             24
#define STATE_DOCTYPE_C             25
#define STATE_DOCTYPE_T             26
#define STATE_DOCTYPE_Y             27
#define STATE_DOCTYPE_P             28
#define STATE_DOCTYPE_E             29

#define TOKEN_NAME                  257
#define TOKEN_COMMENT               258
#define TOKEN_PI_BEGIN              259
#define TOKEN_PI                    260
#define TOKEN_CDATA                 261
#define TOKEN_VALUE                 262
#define TOKEN_END_TAG_BEGIN         263
#define TOKEN_ENDING                264
#define TOKEN_WHITE_SPACE           265
#define TOKEN_CDATA_SECTION         266
#define TOKEN_EOF                   E_EOF
#define TOKEN_DOCTYPE               TOKEN_COMMENT
class CFileStream
{
public:
   CFileStream (int32 nBufSize);
   virtual ~CFileStream ();

   int32 OpenFile (char* pszFilename);
   void CloseFile ();
   
   int32 GetChar (char* pChar);
   int32 PutChar (char c);
   
public:
   int32    m_nLine;
   int32    m_nCol;

protected:
   FILE*    m_hFile;
   char*    m_pBuffer;
   int32    m_nSize;
   int32    m_nCurrent;
   int32    m_nTotalChars;  
   
   int32    m_nPrevLine;
   int32    m_nPrevCol;
   
protected:
   /* helper function. */
   int32 GetCharFromBuffer (char* pChar);   
};

class CXMLScanner 
{
public:
   CXMLScanner (int32 nBufSize);
   virtual ~CXMLScanner ();

   int OpenFile (char* szFilename);
   void CloseFile ();
   int GetToken ();  // Returns token type, 0 = EOF, < 0 ERROR.
   int GetPI ();
   int GetCDATA ();
   
   int GetLineNo ();
   int GetColNo ();
   
   char* m_pToken;
   
protected:
   CFileStream*         m_pStream;
   int32                m_nSize;
   int32                m_nIdx;
// The m_nCol & m_nLine are only used when processing
//  Reference Entities in CDATA and attribute value.    
   int32                m_nCol;     
   int32                m_nLine;
   
protected:
   int32 ValueStringPostProcess (char* szString);
   int32 CDATAStringPostProcess (char* szString);
   int32 CheckReferenceEntities (char* szString, int nSrc, int* pnDest, int* pnAmpPos);
};

#endif /* __XML_PARSER_SCAN_H__ */
